package com.topit.show.aliapi;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.bike.util.UuidUtil;

public class AlyOssFileUpLoadUtils {
	String endpoint = AliConstant.aliyunOssImgUploadUrl;
	String accessKeyId = AliConstant.accessKeyId;
	String accessKeySecret = AliConstant.accessKeySecret;
	String bucketName =AliConstant.BucketName;
	//上传图片
	public List<String> filesUpload(String keypath,MultipartFile[] files){
		List<String> resultList = new ArrayList<String>();
		try{
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			if(files!=null&&files.length>0){
				for(MultipartFile file : files){
					String extName = "";
					if (file.getOriginalFilename().lastIndexOf(".") >= 0){
						extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
					}
					extName =  UuidUtil.get32UUID()+extName;
					ossClient.putObject(bucketName,keypath+extName,file.getInputStream());
					resultList.add(keypath+extName);
				}
			}
			ossClient.shutdown();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}
	public static void main(String[] args) {
		new AlyOssFileUpLoadUtils().fileUpload("shop/imgtou.png", new File("C:\\Users\\grsun\\Pictures\\img_tou.png"));
		new AlyOssFileUpLoadUtils().fileUpload("shop/onload.png", new File("D:\\dizhutest\\wtpwebapps\\wtpwebapps\\hh-erp\\static\\public\\images\\onload.jpg"));
	}
	//上传图片
	public void filesUpload(String keypath,FileItem files){
		try{
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			if(files!=null){
				ossClient.putObject(bucketName,keypath,files.getInputStream());
			}
			ossClient.shutdown();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//上传图片
	public String fileUpload(String keypath,File file){
		String result = null;
		OSSClient ossClient = null;
		try{
			ossClient=new OSSClient(endpoint, accessKeyId, accessKeySecret);
			PutObjectResult result2= ossClient.putObject(bucketName,keypath,new FileInputStream(file));
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(ossClient!=null){
				ossClient.shutdown();
			}
		}
		return result;
	}
	//上传图片
	public String fileUpload(String keypath,MultipartFile file){
		String result = "";
		try{
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			ossClient.putObject(bucketName,keypath,file.getInputStream());
			result = keypath;
			ossClient.shutdown();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	//下载图片到指定文件
	public void fileDownload(String keypath,File file){
		try{
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			ossClient.getObject(new GetObjectRequest(bucketName, keypath),file);
			ossClient.shutdown();
		}catch (Exception e) {
		}
	}
	 
	// 查看文件是否存在
	public boolean doesObjectExist(String keypath){
		try{
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			boolean found = ossClient.doesObjectExist(bucketName, keypath);
			ossClient.shutdown();
			return found;
		}catch (Exception e) {
		}
		return false;
	}
}
